<template>
  <div class="userLoginPage">
    <h2 class="title">用户登陆</h2>
    <a-form
        style="max-width: 480px;margin: 0 auto"
        :model="formState"
        name="basic"
        label-align="left"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 20 }"
        autocomplete="off"
        @finish="handleSubmit"
        @finishFailed="onFinishFailed"
    >
      <a-form-item
          label="账号"
          name="userAccount"
          :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号"/>
      </a-form-item>

      <a-form-item
          label="密码"
          name="userPassword"
          :rules="[{ required: true, message: '请输入密码' }]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 4, span: 20 }">
        <a-button type="primary" html-type="submit">登陆</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
import {reqUserLogin} from "@/api/user.ts";
import {useLoginUserStore} from "@/store/useLoginUserStore.ts";
import {message} from "ant-design-vue";
import {useRouter} from "vue-router";

interface FormState {
  userAccount: string;
  userPassword: string;
}

const formState = reactive<FormState>({
  userAccount: '',
  userPassword: '',
});
const router = useRouter();
const loginUserStore = useLoginUserStore();
//提交表单
const handleSubmit =async (values: any) => {
  const res = await reqUserLogin(values);
  console.log(res.data)
  if(res.data.code === 0){
    await loginUserStore.fetchLoginUser()
    message.success('登陆成功')
    router.push({
      path: '/',
      //替换页面
      replace: true,
    })
  }else{
    message.error(`登陆失败${res.data.message}`)
  }
};

const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo);
};

</script>

<style scoped>
.userLoginPage .title{
  margin: 0 auto;
  text-align: center;
  margin-bottom: 16px;
}
</style>
